{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python JSON"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "\n",
    "\n",
    "JSON 函数\n",
    "使用 JSON 函数需要导入 json 库：import json。\n",
    "\n",
    "函数\t描述\n",
    "json.dumps\t将 Python 对象编码成 JSON 字符串\n",
    "json.loads\t将已编码的 JSON 字符串解码为 Python 对象\n",
    "json.dumps\n",
    "json.dumps 用于将 Python 对象编码成 JSON 字符串。\n",
    "\n",
    "语法\n",
    "json.dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, encoding=\"utf-8\", default=None, sort_keys=False, **kw)\n",
    "\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{\"a\": 1, \"b\": 2, \"c\": 3, \"d\": 4, \"e\": 5, \"f\": 6}]\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "\n",
    "# 将数组编码为 JSON 格式数据：\n",
    "data = [{'a':1, 'b':2, 'c':3, 'd':4, 'e':5, 'f':6}]\n",
    "\n",
    "jsonData = json.dumps(data)\n",
    "print(jsonData)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "\n",
    "python 原始类型向 json 类型的转化对照表：\n",
    "\n",
    "Python\t           JSON\n",
    "\n",
    "dict\t           object\n",
    "\n",
    "list,              tuple\tarray\n",
    "\n",
    "str, unicode\t   string\n",
    "\n",
    "int, long, float   number\n",
    "\n",
    "True\t           true\n",
    "\n",
    "False\t           false\n",
    "\n",
    "None\t           null\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6}\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "\n",
    "json.loads\n",
    "json.loads 用于解码 JSON 数据。该函数返回 Python 字段的数据类型。\n",
    "\n",
    "语法\n",
    "json.loads(s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])\n",
    "\n",
    "'''\n",
    "\n",
    "# 展示了Python 如何解码 JSON 对象：\n",
    "\n",
    "jsonData = '{\"a\": 1, \"b\": 2, \"c\": 3, \"d\": 4, \"e\": 5, \"f\": 6}';\n",
    "\n",
    "text = json.loads(jsonData)\n",
    "print(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{\"a\":1,\"b\":2,\"c\":3,\"d\":4,\"e\":5}]\n"
     ]
    }
   ],
   "source": [
    "import demjson\n",
    "\n",
    "\n",
    "# 将数组编码为 JSON 格式数据：\n",
    "data = [ { 'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4, 'e' : 5 } ]\n",
    "\n",
    "json = demjson.encode(data)\n",
    "print(json)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "\n",
    "\n",
    "decode\n",
    "Python 可以使用 demjson.decode() 函数解码 JSON 数据。该函数返回 Python 字段的数据类型。\n",
    "\n",
    "语法\n",
    "demjson.decode(self, txt)\n",
    "\n",
    "\n",
    "'''\n",
    "\n",
    "json = '{\"a\":1,\"b\":2,\"c\":3,\"d\":4,\"e\":5}'\n",
    "\n",
    "text = demjson.decode(json)\n",
    "print(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
